package com.lhf.aicloud.task;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lhf.aicloud.entity.User;
import com.lhf.aicloud.service.IUserService;
import com.xxl.job.core.handler.annotation.XxlJob;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @author: lhf
 * @date: Created in 2024/08/17
 * @description: 定时任务
 */
@Slf4j
@Component
public class TimeTask {

    @Value("${user.use-count}")
    private Integer useCount;

    @Resource
    private IUserService userService;

    /**
     * 定时重置用户使用次数
     */
    @XxlJob("resetUserUseCountHandler")
    public void resetUserUseCountHandler() {
        log.info("开始重置用户使用次数");
        boolean result = userService.update(
                Wrappers.lambdaUpdate(User.class).ne(User::getUsername, "admin")
                        .set(User::getUsecount, useCount)
        );
        if (result) {
            log.info("重置用户使用次数成功");
        } else {
            // todo 通知负责人排查问题
            log.info("重置用户使用次数失败");
        }
    }
}
